﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace RVBike
{
    /// <summary>
    /// Interaction logic for RVPageSwitcher.xaml
    /// </summary>
    public partial class RVPageSwitcher : Window
    {
        public RVPageSwitcher()
        {
            InitializeComponent();
            RVSwitcher.Instance = this;
            RVSwitcher.Switch(new WelcomePage());
        }

        public void Navigate(UserControl nextPage)
        {
            this.Content = nextPage;
        }

        public void Navigate(UserControl nextPage, object state)
        {
            this.Content = nextPage;
            ISwitchable s = nextPage as ISwitchable;

            if (s != null)
            {
                s.UtilizeState(state);
            }
            else
            {
                throw new ArgumentException("NextPage is not ISwitchable! "
                    + nextPage.Name.ToString());
            }
        }

        private void OnLocationChanged(object sender, EventArgs e)
        {
            DisplayEnv.Instance().WindowLeft = this.Left;
            DisplayEnv.Instance().WindowTop = this.Top;
            DisplayEnv.Instance().WindowWidth = this.Width;
            DisplayEnv.Instance().WindowHeight = this.Height;
            ISwitchable s = this.Content as ISwitchable;
            if (s != null)
            {
                s.WindowLocationChanged(this.Left, this.Top);
            }
        }

        // Close Client/Server when ap closed
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            RVGameController.Instance().LeaveRoom();
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            DisplayEnv.Instance().WindowLeft = this.Left;
            DisplayEnv.Instance().WindowTop = this.Top;
            DisplayEnv.Instance().WindowWidth = this.Width;
            DisplayEnv.Instance().WindowHeight = this.Height;
            ISwitchable s = this.Content as ISwitchable;
            if (s != null)
            {
                s.WindowLocationChanged(this.Left, this.Top);
            }
        }
    }
}
